import request from '@/utils/request'
// import { getToken } from '@/utils/token'

/**
 * 登录
 * @param {*} param0 数据对象
 * @returns Promise
 */
export const loginAPI = ({ mobile, code }) => {
  return request({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile, code
    }
  })
}

export const refreshTokenAPI = () => {
  return request({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: 'Bearer ' + localStorage.getItem('refresh_token')
    }
  })
}
/**
 * 获取已选频道
 * @returns
 */
export const getUserChannelListAPI = () => {
  return request({
    url: '/v1_0/user/channels'
    // headers: {
    //   Authorization: 'Bearer ' + getToken()
    // }
  })
}

/**
 * 获取首页文章列表
 * @param {*} channel_id
 * @returns
 */
export const getArticleListAPI = ({ channel_id, timestamp = new Date().getTime() }) => {
  return request({
    url: '/v1_0/articles',
    // headers: {
    //   Authorization: 'Bearer ' + getToken()
    // },
    params: {
      channel_id,
      timestamp
    }
  })
}

/**
 * 首页-反馈-不喜欢文章
 * @param {*} artId 文章ID
 * @returns
 */
export const dislikeArticleAPI = (artId) => {
  return request({
    url: 'v1_0/article/dislikes',
    method: 'POST',
    // headers: {
    //   Authorization: 'Bearer ' + getToken()
    // },
    data: {
      target: artId
    }
  })
}

/**
 * 举报文章
 * @param {*} param0
 * @returns
 */
export const articleReportsAPI = ({ target, type, remark }) => {
  return request({
    url: 'v1_0/article/reports',
    method: 'POST',
    // headers: {
    //   Authorization: 'Bearer ' + getToken()
    // },
    data: {
      target,
      type,
      remark
    }
  })
}
/**
 * 获取所有频道
 * @returns
 */
export const getChannelListAPI = () => {
  return request({
    url: '/v1_0/channels'
  })
}

/**
 *更新用户已选频道列表
 * @param {*} channelArr
 * @returns
 */
export const updateChannelAPI = (channelArr) => {
  return request({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels: channelArr
    }
  })
}

/**
 * 删除频道
 * @param {*} channelId
 * @returns
 */
export const removeChannelAPI = (channelId) => {
  return request({
    url: '/v1_0/user/channels/' + channelId,
    method: 'DELETE'

  })
}

/**
 * 搜索 - 联想菜单
 * @param {*} q 联想关键字
 * @returns
 */
export const suggestListAPI = (q) => {
  return request({
    url: '/v1_0/suggestion',
    params: {
      q: q
    }
  })
}

/**
 * 搜索结果
 * @param {*} param0
 * @returns
 */
export const searchResultAPI = ({ keyWords, page = 1, per_page = 10 }) => {
  return request({
    url: '/v1_0/search',
    params: {
      q: keyWords,
      page: page,
      per_page: per_page
    }
  })
}

/**
 * 获取文章详情
 * @param {*} artId
 * @returns
 */
export const getArticleDetailAPI = (artId) => {
  return request({
    url: '/v1_0/articles/' + artId
  })
}

// 文章 -- 关注作者
export const followedUserAPI = (autId) => {
  return request({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target: autId
    }
  })
}

// 文章 -- 取消关注作者
export const unFollowedUserAPI = (autId) => {
  return request({
    url: `/v1_0/user/followings/${autId}`,
    method: 'DELETE'
  })
}

// 文章 -- 点赞
export const likeArticleAPI = (artId) => {
  return request({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: artId
    }
  })
}

// 文章 -- 取消点赞
export const unLikeArticleAPI = (artId) => {
  return request({
    url: `/v1_0/article/likings/${artId}`,
    method: 'DELETE'
  })
}

// 评论 - 获取文章评论列表
export const getCommentsListAPI = ({ artId, offset = null, limit = 10 }) => {
  return request({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: artId,
      offset, // axios遇到null时 会忽略当前的参数key和值null
      limit
    }
  })
}

// 对文章评论 - 点赞
export const likeCommentAPI = (comId) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}

// 对文章评论 - 取消点赞
export const unLikeCommentAPI = (comId) => {
  return request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}

// 文章 - 发表评论
export const sendCommentAPI = ({ target, content, artId = null }) => {
  const dataObj = {
    target: target, // 文章ID或评论ID
    content: content
  }
  if (artId !== null) {
    // 如果外面传入artId, 证明是要对评论进行回复, 需要第三个参数
    // 对评论进行回复, 我才需要添加文章ID字段和值
    // 如果target是评论的ID(对评论进行回复), 才需要携带文章ID, 如果target是文章的ID(无需此参数)
    dataObj.art_id = artId
  }
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: dataObj // axios会忽略params参数值为null的情况, data方式请求体传递null, axios是不会忽略这对参数的
  })
}

// 获取用户信息
export const getUserInfoAPI = () => {
  return request({
    url: '/v1_0/user'
  })
}

// 获取用户个人资料(包含生日, 用户名, 头像)
export const getProfileAPI = () => {
  return request({
    url: '/v1_0/user/profile'
  })
}

// 修改用户头像
export const updateAvatarAPI = (fd) => {
  return request({
    url: '/v1_0/user/photo',
    method: 'PATCH', // 局部更新, PUT一般是全都更新
    data: fd
    // photo 是表单对象
    // 如果你的请求体内容是表单对象, 浏览器会自动携带请求头Content-Type为multipart/form-data
  })
}

// 修改用户个人资料
export const editProfileAPI = (profile) => {
  return request({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: profile
  })
}
